/*eslint-disable*/

const { resolve } = require('path')
const fs = require('fs')
const glob = require('glob')
const R = require('ramda')
const Promise = require('bluebird')
const paths = require('../build_conf/paths')
const oss = require('../utils/oss')
const qiniu = require('../utils/qiniu')
const appPackage = require(paths.appPackageJson)

const APPLICATIONDIR = `${appPackage.name}/${appPackage.version}`

// 根据具体情况增加扩展名
const filesExt = '(js|css|json|ico|svg|jpg|png)'

const upload = uploadFn => async (file) => {
  const filename = file
    .split(paths.appBuild)[1]
    .slice(1)

  return uploadFn(file, `${APPLICATIONDIR}/${filename}`)
}

const ossUpload = R.applyTo(oss.upload, upload)
const qiniuUpload = R.applyTo(qiniu.upload, upload)
const start = async () => {
  const files = glob.sync(resolve(paths.appBuild, `**/*.+${filesExt}`))

  try {
    const task = await Promise.all([
      ...R.map(qiniuUpload, files),
      ...R.map(ossUpload, files)
    ])
    console.log(task)
    process.exit(0)
  } catch(e) {
    console.log(e)
    process.exit(1)
  }

}

start()
